package com.hang.controller;

import com.hang.common.Result;
import com.hang.exception.MyException;
import com.hang.model.dto.base.ConditionDTO;
import com.hang.model.dto.request.*;
import com.hang.model.vo.TaskInfoDetailVO;
import com.hang.model.vo.TaskInfoVOList;
import com.hang.service.ITaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "任务管理模块")
@RequestMapping("task")
@RestController
@Slf4j
public class TaskController {
    @Resource
    private ITaskService taskService;

    @ApiOperation(value = "发布任务")
    @PostMapping("publish")
    public Result<Object> publishTask(
            @RequestBody @Valid TaskModelReq taskModelReq
    ){
        try {
            taskService.publishTask(taskModelReq);
            return Result.ok().message("发布成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "任务大厅查看未被接单的任务")
    @PostMapping("hall")
    public Result<TaskInfoVOList> getIncompleteTaskInHall(
            @RequestBody ItemConditionDTO conditionDTO
    ){
        TaskInfoVOList taskInfoVOList = taskService.getIncompleteTaskInHall(conditionDTO);
        return Result.ok(taskInfoVOList);
    }

    @ApiOperation(value = "按条件搜索任务")
    @PostMapping("getTasksByCondition")
    public Result<TaskInfoVOList> getTasksByCondition(
            @RequestBody @Valid TaskConditionDTO taskConditionDTO
    ){
        TaskInfoVOList taskInfoVOList = taskService.getTaskListByCondition(taskConditionDTO);
        return Result.ok(taskInfoVOList);
    }

    @ApiOperation(value = "更新任务信息")
    @PostMapping("update")
    public Result<Object> updateTask(
            @RequestBody @Valid TaskModelReq taskModelReq
    ){
        try {
            taskService.updateTask(taskModelReq);
            return Result.ok().message("更新成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "批量删除任务")
    @DeleteMapping("deleteBatch")
    public Result<Object> deleteTask(
            @RequestBody @Valid TaskDeleteDTO taskDeleteDTO
    ){
        try {
            taskService.deleteTaskList(taskDeleteDTO);
            return Result.ok().message("删除成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "查看任务详情")
    @GetMapping("detail")
    public Result<TaskInfoDetailVO> getTaskDetail(
            @RequestParam("taskId") Long taskId
    ){
        TaskInfoDetailVO taskInfoDetailVO = taskService.getDetail(taskId);
        return Result.ok(taskInfoDetailVO);
    }

    @ApiOperation(value = "修改任务处理状态[处理任务]")
    @PostMapping("updateStatus")
    public Result<Object> updateTaskStatus(
            @RequestBody @Valid TaskUpdateReq taskUpdateReq
    ){
        try {
            taskService.updateTaskStatus(taskUpdateReq);
            return Result.ok().message("已修改任务状态");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "任务接单")
    @PostMapping("receive")
    public Result<Object> receivingOrders(
            @RequestBody @Valid TaskReceiveDTO taskReceiveDTO
    ){
        try {
            taskService.receivingTask(taskReceiveDTO);
            return Result.ok().message("已将该任务添加到你的待处理任务中");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

}
